package com.itheima.controller;

import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 查询健康管理师
     *
     * @return
     */
    @RequestMapping("/names")
    public Result getManagerNames() {
        List<Map<String, Object>> list = memberService.findManagerByRoleId();
        return Result.success(list, "查询健康管理师成功");
    }

    /**
     * 分页条件查询会员信息
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult Page(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = memberService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 新增会员
     *
     * @param member
     * @return
     */
    @PostMapping
    public Result addMember(@RequestBody Member member) {
        memberService.add(member);
        return Result.success("新增会员成功");
    }

    /**
     * 删除会员
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteMember(@PathVariable Integer id) {
        memberService.deleteById(id);
        return Result.success("删除会员成功");
    }

    /**
     * 根据id查询员工
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result queryById(@PathVariable Integer id) {
        Member member = memberService.findById(id);
        return Result.success(member, "查询员工数据成功");
    }

    /**
     * 编辑会员
     *
     * @param member
     * @return
     */
    @PutMapping
    public Result updateMember(@RequestBody Member member) {
        memberService.updateById(member);
        return Result.success("编辑会员成功");
    }

    /**
     * 导出会员
     *
     * @param response
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping("/exportExcel")
    public Result exportExcel(HttpServletResponse response, HttpServletRequest request) throws Exception {
        try {
            XSSFWorkbook excel = new XSSFWorkbook();
            //创建第一个工作表
            XSSFSheet sheet = excel.createSheet();
            XSSFRow row1 = sheet.getRow(0);
            if (row1 == null) {
                row1 = sheet.createRow(0);
            }
            if (row1.getCell(0) == null) {
                row1.createCell(0);
                row1.getCell(0).setCellValue("档案号");
            }
            if (row1.getCell(1) == null) {
                row1.createCell(1);
                row1.getCell(1).setCellValue("姓名");
            }
            if (row1.getCell(2) == null) {
                row1.createCell(2);
                row1.getCell(2).setCellValue("性别");
            }
            if (row1.getCell(3) == null) {
                row1.createCell(3);
                row1.getCell(3).setCellValue("年龄");
            }
            if (row1.getCell(4) == null) {
                row1.createCell(4);
                row1.getCell(4).setCellValue("健康管理师");
            }
            if (row1.getCell(5) == null) {
                row1.createCell(5);
                row1.getCell(5).setCellValue("注册时间");
            }
            if (row1.getCell(6) == null) {
                row1.createCell(6);
                row1.getCell(6).setCellValue("手机号码");
            }
            if (row1.getCell(7) == null) {
                row1.createCell(7);
                row1.getCell(7).setCellValue("套餐");
            }
            List<Member> list = memberService.findAll();
            int j = 1;
            for (int i = 0; i < list.size(); i++) {
                XSSFRow row = sheet.getRow(j++);
                if (row == null) {
                    row = sheet.createRow(j - 1);
                }
                Member member = list.get(i);
                if (row.getCell(0) == null) {
                    row.createCell(0);
                    row.getCell(0).setCellValue(member.getFileNumber());
                }
                if (row.getCell(1) == null) {
                    row.createCell(1);
                    row.getCell(1).setCellValue(member.getName());
                }
                if (row.getCell(2) == null) {
                    row.createCell(2);
                    row.getCell(2).setCellValue(member.getSex());
                }
                if (row.getCell(4) == null) {
                    row.createCell(4);
                    row.getCell(4).setCellValue(member.getManager());
                }
                if (row.getCell(5) == null) {
                    row.createCell(5);
                    row.getCell(5).setCellValue(member.getRegTime());
                }
                if (row.getCell(6) == null) {
                    row.createCell(6);
                    row.getCell(6).setCellValue(member.getPhoneNumber());
                }
            }

            //使用输出流进行表格下载,基于浏览器作为客户端下载
            OutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");//代表的是Excel文件类型 content-type  application/vnd.ms-excel
            // response.setHeader("Content-Type","application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=test.xlsx");//指定以附件形式进行下载 强制下载！！
            excel.write(out);

            out.flush();
            out.close();
            excel.close();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("下载失败");
        }
    }
}